package com.lfs.market.controller.front;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.lfs.market.exception.ServiceException;
import com.lfs.market.model.R;
import com.lfs.market.model.dto.CreateCommentDto;
import com.lfs.market.model.enums.ResultCode;
import com.lfs.market.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/comment")
@SaCheckLogin
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 保存评论信息
     *
     * @param dto
     * @return
     */
    @PostMapping
    public R saveComment(@RequestBody CreateCommentDto dto) {
        commentService.saveComment(dto.getProductId(), dto.getParentId(), dto.getContent());
        return R.ok();
    }

    /**
     * 根据商品id获取评论信息
     *
     * @param productId
     * @return
     */
    @GetMapping("/byProduct")
    public R<Map> getCommentList(@RequestParam("productId") String productId) {
        HashMap map = commentService.getCommentList(productId);
        return R.ok(map);
    }

    /**
     * 删除评论
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delComment(@PathVariable("id") String id) {
        boolean update = commentService.removeById(id);
        if (!update) throw new ServiceException(ResultCode.DeleteError);
        return R.ok();
    }
}
